/*
 * Proja - project management software
 * Copyright (C) 2012  Oleg Kasian
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package org.proja.core.entity;

import java.util.Date;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.proja.core.entity.enumerations.UserStatus;

@Entity(name = "User")
@Table(name = "USERS")
public class User extends AbstractEntity {
    
    @Id
    @SequenceGenerator(name = "UserSeq", sequenceName = "USERS_SEQ")
    @GeneratedValue(generator = "UserSeq")
    @Column(name = "ID", updatable = false)
    private Long id;

    @Column(name = "LOGIN", updatable = false, nullable = false)
    private String login;

    @Column(name = "PASSWORD")
    private String password;

    @Column(name = "SALT")
    private String salt;

    @Column(name = "FIRSTNAME", nullable = false)
    private String firstName;

    @Column(name = "LASTNAME", nullable = false)
    private String lastName;

    @Column(name = "MAIL", nullable = false)
    private String mail;

    @Column(name = "STATUS", nullable = false)
    private UserStatus status = UserStatus.REGISTERED;

    @Column(name = "LAST_LOGIN")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastLogin = new Date();
    
    @Column(name = "CREATED_ON", nullable = false, updatable = false)
    @Temporal(TemporalType.DATE)
    private Date createdOn = new Date();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Set<UserSetting> settings;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
    private Set<Project> ownedProjects;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Set<Token> tokens;
    
    @ManyToMany(cascade = CascadeType.DETACH)
    @JoinTable(name = "USERS_USER_ROLES",
            joinColumns = { @JoinColumn(name = "USER_ID") },
            inverseJoinColumns = { @JoinColumn(name = "USER_ROLE_ID") })
    private Set<UserRole> roles;
    
    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(final Long id) {
        this.id = id;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(final String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(final String salt) {
        this.salt = salt;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(final String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(final String lastName) {
        this.lastName = lastName;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(final String mail) {
        this.mail = mail;
    }

    public UserStatus getStatus() {
        return status;
    }

    public void setStatus(final UserStatus status) {
        this.status = status;
    }

    public Set<UserSetting> getSettings() {
        return settings;
    }

    public void setSettings(final Set<UserSetting> settings) {
        this.settings = settings;
    }

    public Date getLastLogin() {
        return lastLogin;
    }

    public void setLastLogin(final Date lastLogin) {
        this.lastLogin = lastLogin;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(final Date createdOn) {
        this.createdOn = createdOn;
    }

    public Set<Project> getOwnedProjects() {
        return ownedProjects;
    }

    public void setOwnedProjects(final Set<Project> ownedProjects) {
        this.ownedProjects = ownedProjects;
    }

    public Set<UserRole> getRoles() {
        return roles;
    }

    public void setRoles(final Set<UserRole> roles) {
        this.roles = roles;
    }

    public Set<Token> getTokens() {
        return tokens;
    }

    public void setTokens(final Set<Token> tokens) {
        this.tokens = tokens;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 79 * hash + (this.login != null ? this.login.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.login == null) ? (other.login != null) : !this.login.equals(other.login)) {
            return false;
        }
        return true;
    }

}
